import { createStore, applyMiddleware, combineReducers } from 'redux';
import createSagaMiddleware from 'redux-saga';
import { all } from 'redux-saga/effects';

import homeReducer from '@src/model/home/reducer';
import transactionReducer from '@src/model/transaction-statistics/reducer';
import latestBlockReducer from '@src/model/newest-block-info/reducer';
import blockDetailReducer from '@src/model/block-detail/reducer';
import blockListReducer from '@src/model/block-list/reducer';
import globalReducer from '@src/model/global/reducer';
import txListReducer from '@src/model/transaction-list/reducer';
import txDetailReducer from '@src/model/transaction-detail/reducer';

import homeSaga from '@src/model/home/saga';
import transactionSaga from '@src/model/transaction-statistics/saga';
import latestBlockSaga from '@src/model/newest-block-info/saga';
import blockDetailSaga from '@src/model/block-detail/saga';
import blockListSaga from '@src/model/block-list/saga';
import txListSaga from '@src/model/transaction-list/saga';
import txDetailSaga from '@src/model/transaction-detail/saga';

const rootReducer = combineReducers({
  globalReducer,
  homeReducer,
  transactionReducer,
  latestBlockReducer,
  blockDetailReducer,
  blockListReducer,
  txListReducer,
  txDetailReducer,
});

function* rootSaga() {
  yield all([
    ...homeSaga,
    ...transactionSaga,
    ...latestBlockSaga,
    ...blockDetailSaga,
    ...blockListSaga,
    ...txListSaga,
    ...txDetailSaga,
  ]);
}

const sagaMiddleware = createSagaMiddleware();
const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));

sagaMiddleware.run(rootSaga);
export { store };
